package com.xiaou.buss.admin.manageruser.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.criteria.Criteria;
import com.base.service.BaseServiceImpl;
import com.xiaou.buss.admin.manageruser.dao.ManagerUserDao;
import com.xiaou.buss.admin.manageruser.entity.ManagerUser;
import com.xiaou.buss.admin.manageruser.entity.ManagerUserBO;
import com.xiaou.buss.admin.manageruser.service.ManagerUserService;
import com.xiaou.component.common.Page;
import com.xiaou.component.common.XLTCommon;
import com.xiaou.component.utils.MD5Util;

/**
 * 
 * <br>
 * <b>功能：</b>ManagerUserService<br>
 */
@Service
public class  ManagerUserServiceImpl  extends BaseServiceImpl implements ManagerUserService {
  Logger logger = LoggerFactory.getLogger(getClass());
	

	@Autowired
    private ManagerUserDao dao;

		
	public ManagerUserDao getDao() {
		return dao;
	}
	
	@Override
	public ManagerUser findLogin(String userName, String pwd) {
		ManagerUser user = new ManagerUser();

		// 判断是有的是什么账号登陆,匹配正则表达式
		if (userName.matches(XLTCommon.EMAIL)) {// 邮箱登录
			if (logger.isInfoEnabled()) {
				logger.trace("----" + userName + "----正在使用邮箱登录----");
			}
			user.setEmail(userName);

		} else if (userName.matches(XLTCommon.MOBILE)) {// 手机号登录
			if (logger.isInfoEnabled()) {
				logger.trace("----" + userName + "----正在使用手机号登录----");
			}
			user.setMobile(userName);
		} else {
			if (logger.isInfoEnabled()) {
				logger.trace("----" + userName + "----正在使用用户名登录----");
			}
			user.setAccount(userName);
		}
		String encodedPwd = MD5Util.md5(pwd);
		user.setPassword(encodedPwd);
		
		return dao.findlogin(user);
	}




	@Override
	public List<Map<String, Object>> selectRoleUser(String roleId,String name,String account) {
		ManagerUserBO user = new ManagerUserBO();
		user.setRoleId(roleId);
		if(StringUtils.isNotBlank(name)){
			user.setName("%"+name+"%");
		}
		if(StringUtils.isNotBlank(account)){
			user.setAccount(account);
		}
		
		return dao.selectRoleUser(user);
	}




	@Override
	public int findUserTotalCount(String name,String account) {
		Criteria cta = new Criteria();
		if(StringUtils.isNotBlank(name)){
			cta.put("name", "%"+name+"%");
		}if(StringUtils.isNotBlank(account)){
			cta.put("account", account);	
		}
		
		return dao.findUserTotalCount(cta);
	}

	@Override
	public List<ManagerUser> findUser(String name,String account,Page page) {
		Criteria cta = new Criteria();
		if(StringUtils.isNotBlank(name)){
			cta.put("name", "%"+name+"%");
		}if(StringUtils.isNotBlank(account)){
			cta.put("account", account);	
		}
		cta.setCurrentPage(page.getPageNo());
		cta.setPageSize(page.getPageSize());
		return dao.findUser(cta);
	}


	@Override
	public int selectRoleUserTotal(String roleId) {
		return dao.selectRoleUserTotal(roleId);
	}

	@Override
	public List<ManagerUser> selectNotRoleUser(String roleId) {
		return dao.selectNotRoleUser(roleId);
	}




	@Override
	@Transactional
	public void batchDeleteUser(String ids) {
		if(StringUtils.isNotBlank(ids)){
			String[] userId=ids.split(",");
			
			for (String uid : userId) {
				ManagerUser user = new ManagerUser();
				user.setId(uid);
				user.setState(2);
				dao.updateBySelective(user);
			}
		}
	}
}
